package com.mybatis.test.plugin;

import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Optional;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author Administrator
 * @create 2020-08-12 16:47
 */
@Intercepts({@Signature(
        type = Executor.class,
        method = "update",
        args = {MappedStatement.class, Object.class}
)})
public class MyPlugin implements Interceptor {
    private static final Logger LOGGER = LoggerFactory.getLogger(MyPlugin.class);
    Properties properties;

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        LOGGER.info("before...");
        Object obj = invocation.proceed();
        LOGGER.info("after...");
        return obj;
    }

    @Override
    public Object plugin(Object target) {
        LOGGER.warn("调用生成代理对象...");
        return Plugin.wrap(target, this);
    }

    @Override
    public void setProperties(Properties properties) {
        Optional.ofNullable(properties.get("dbType")).ifPresent(p -> LOGGER.info(p.toString()));
        this.properties = properties;
    }
}
